<?php
namespace app\index\controller;

use app\common\controller\HomeBase;  // model
use think\Db;                       // db
use app\common\model\Wacategory as WacategoryModel;
use app\common\model\Wenan as WenanModel;

class Index extends HomeBase
{
    protected $wacategory_model;
    protected $wenan_model;

    protected function _initialize(){
        parent::_initialize();
        $this->wacategory_model = new WacategoryModel();
        $this->wenan_model = new WenanModel();
        $category_level_list = $this->wacategory_model->getLevelList();
        $this->assign('category_level_list',$category_level_list);
    }

    public function docs(){
        // 根据分类筛选显示文案  显示  wenan 里面的所有数据 和 分类数据里面的分类名称分开
        $result = $this->wenan_model->alias('w')->field('w.*,think_wacategory.name')->where('status',1)->join('think_wacategory','w.wid = think_wacategory.id')->order('w.sort desc')->select();
        $this->assign('wenan_list',$result);
        $this->assign('title','文案列表');
        return $this->fetch();
    }

    public function docst(){
        $result = $this->wenan_model->alias('w')->field('w.*,think_wacategory.name')->where('status',1)->join('think_wacategory','w.wid = think_wacategory.id')->order('w.sort desc')->select();
        dump($result);
//        $this->assign('wenan_list',$result);
//        $this->assign('title','文案列表');
//        return $this->fetch();
    }

    public function index()
    {
        $this->assign('title','首页');
        return $this->fetch();
    }

    public function i( $keyword = '', $page = 1){
        $map   = [];
        $field = 'id,title,wid,author,reading,status,publish_time,sort';

        // 筛选分类
        if (!empty($keyword)) {
            $map['title'] = ['like', "%{$keyword}%"];
        }

        $result = $this->wenan_model->field($field)->where($map)->order(['publish_time' => 'DESC'])->paginate(15, false, ['page' => $page]);

        $wecategory = $this->wacategory_model->column('name', 'id');

//        $result = $this->wenan_model->alias('w')->field('w.*,think_wacategory.name')->where('status',1)->join('think_wacategory','w.wid = think_wacategory.id')->order('w.sort desc')->select();
//        $this->assign('wenan_list',$result);
//        $this->assign('title','文案列表');
        return $this->fetch('i', ['wecategory_list' => $wecategory, 'wenan_list' => $result, 'keyword' => $keyword,'title','文案列表']);
    }
}
/*
 * CREATE TABLE `think_wenan` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `wid` int(11) NOT NULL,
  `wenan_path` varchar(255) NOT NULL,
  `compress` varchar(255) NOT NULL,
  `title` varchar(255) DEFAULT NULL,
  `thumb` varchar(255) DEFAULT NULL,
  `remark` varchar(255) DEFAULT NULL,
  `sort` int(11) DEFAULT '0',
  `create_time` datetime NOT NULL,
  `status` tinyint(1) DEFAULT '1',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=162 DEFAULT CHARSET=utf8;
*/






